// -*- c++ -*-
/**************************************************************/
/*  This file is licensed from, and is a trade secret of:     */
/*                                                            */
/*    Mercury Computer Systems, Incorporated                  */
/*    199 Riverneck Road                                      */
/*    Chelmsford, Massachusetts 01824-2820                    */
/*    United States of America                                */
/*    Telephone + 1 978 256-1300                              */
/*                                                            */
/*  Refer to your Software License Agreement for provisions   */
/*  on use, duplication, or disclosure.                       */
/*  US. Government users and prime contractors are also       */
/*  governed by the following Data Rights Notice:             */
/*  Use, duplication or disclosure by the US. Government is   */
/*  subject to the provisions set forth in FAR 52.227-14 for  */
/*  restricted rights or other agency data rights provisions  */
/*  on commercial software and documentation, as applicable.  */
/*                                                            */
/*  This file: (a) was developed at private expense, and no   */
/*  part was developed with government funds, (b) is a        */
/*  trade secret of Mercury Computer Systems, Inc. for the    */
/*  purposes of the Freedom of Information Act, (c) is        */
/*  "commercial computer software" subject to limited         */
/*  utilization as provided in the above noted Software       */
/*  License Agreement and (d) is in all respects data         */
/*  belonging exclusively to Mercury Computer Systems, Inc..  */
/*                                                            */
/**************************************************************/

/******************************************************************/
/* Copyright ¿ 1999-2003, Mercury Computer Systems, Inc.,         */
/* Chelmsford MA.,and all third party embedded software sources.  */
/* All rights reserved under the Copyright laws of US. and        */
/* international treaties.                                        */
/*                                                                */
/* Patent Pending                                                 */
/******************************************************************/


//$COPYRIGHT_END


#ifndef MC_COMPONENT_FILE_INPUT_H
#define MC_COMPONENT_FILE_INPUT_H

#include <iostream>
#include <string>
#include "CF__Resource_impl.h"
#include "AudioPort_impl.h"
#include "SceOsSemaphore.h"
#include "SceOsThreadManager.h"
#include "mac.h"
#include "mac_port.h"
#include "mc_error.h"

#ifdef _WIN32
#ifdef FILEIO_MAC_LLC_EXPORTS
#define FILEIO_MAC_LLC_EXPORT __declspec(dllexport)
#else
#define FILEIO_MAC_LLC_EXPORT __declspec(dllimport)
#endif
#else
#define FILEIO_MAC_LLC_EXPORT 
#endif

#define MAX_FILE_NAME 256
#define MAX_DATA_TYPE_LEN 16

class FILEIO_MAC_LLC_EXPORT FileInput_MAC_LLC : public CF__Resource_impl {
public: 
  static status
  Create (FileInput_MAC_LLC *& ModulatorHandle,
	  const char * identifier);

  // Error control methods.
  void signalNAK (SDR_FM3TR::MAC_Signaling::PacketID error_packet);

  // Flow control methods.
  // From POA_SDR_FM3TR::MAC_Signaling::MAC_FlowControl_Local
  void signalHighWatermark ();
  void signalLowWatermark ();
  void signalEmpty ();

  // From CF::Resource
  void start ()
    throw (CORBA::SystemException, CF::Resource::StartError);
  void stop ()
    throw (CORBA::SystemException, CF::Resource::StopError);

  // From CF::PortSupplier
  CORBA::Object_ptr getPort (const CORBA::Char * name)
    throw (CORBA::SystemException, CF::PortSupplier::UnknownPort);

private:
  FileInput_MAC_LLC (const char *identifier);
  ~FileInput_MAC_LLC ();

  status Init ();
  status AssembleHeader ();
  status AssemblePacket (unsigned int packetID, int & num_samples);
  status AssembleDummyPacket ();

  // Inner loop method that performs component business logic.
  status Run ();
  // Separate thread master function.
  static void RunWorker (void * param);
  // Inner loop function that deals with external commands.
  status RunLoop ();


  // Data port definition.
  FileOutPort_impl * m_fileout;
  // Provides Interface definitions.
  MAC_ErrCtrl_XmitInPort_impl<FileInput_MAC_LLC> * m_mac_err_in;
  MAC_FlowCtrl_InPort_impl<FileInput_MAC_LLC> * m_mac_flow_ctrl_in;
  // Uses Interface definitions.
  MAC_Ctrl_OutPort_impl<SDR_FM3TR::LLC_Control> * m_llc_ctrl_out;

  bool m_bPauseFlow;

  unsigned int m_output_count;
  char * m_output;

  short * m_mac_input;
  unsigned int m_input_count;

  bool m_bStop;
  bool m_threadrunning;
  SCE::OS::ThreadManager m_thread;

  FILE * m_file_in;
  size_t m_data_size;

  unsigned short m_num_packets;
  unsigned short m_packetCounter;
  unsigned short m_num_bytes_last_packet;
  bool m_eof;
  bool m_header_sent;

  // Error packet variables.
  short * m_errorPackets;
  short m_numErrorPackets;

  unsigned char * m_header;

  // Properties.
  const char * m_prop_input_file_name;
  const char * m_prop_input_data_type;
  unsigned short m_num_data_elems;
};

class FILEIO_MAC_LLC_EXPORT FileOutput_MAC_LLC : public CF__Resource_impl {
public:
  static status
  Create (FileOutput_MAC_LLC *& DemodHandle,
	  const char * identifier);

  status RewritePacket (short packetID);

  // Error control methods
  void signalACK ();
  void signalNAK ();

  // From CF::Resource
  void start ()
    throw (CORBA::SystemException, CF::Resource::StartError);
  void stop ()
    throw (CORBA::SystemException, CF::Resource::StopError);

  // From CF::PortSupplier
  CORBA::Object_ptr getPort (const CORBA::Char * name)
    throw (CORBA::SystemException, CF::PortSupplier::UnknownPort);

private:
  FileOutput_MAC_LLC (const char *identifier);
  ~FileOutput_MAC_LLC ();

  status Init ();
  status ExtractHeader ();

  // Inner loop method that performs component business logic.
  status Run ();
  // Separate thread master function.
  static void RunWorker (void * param);
  // Inner loop function that deals with external commands
  status RunLoop ();


  // Port definitions
  FileInPort_impl * m_filein;
  MacInPort_impl * m_macin;

  // Provides Interface definitions
  MAC_ErrCtrl_ReceiveInPort_impl<FileOutput_MAC_LLC> * m_mac_err_ctrl_in;

  unsigned int m_input_count;
  char * m_input;

  unsigned int mac_buffer_size;
  short * m_mac_input;

  unsigned int m_total_output_count;
  int m_rewritePacketIndex;

  bool m_bStop;
  bool m_threadrunning;
  SCE::OS::ThreadManager m_thread;

  FILE * m_file_out;
  enum IODataType m_data_type;

  unsigned char * m_header;
  unsigned short m_num_packets;
  unsigned short m_packetCounter;
  unsigned short m_num_bytes_last_packet;

  // Error packet variables.
  unsigned short m_packetFeedbackCounter;
  short * m_errorPackets;
  short m_numErrorPackets;
  bool m_retransmissionMode;

  bool m_header_received;

  // Properties.
  const char * m_prop_output_file_name;
  const char * m_prop_output_data_type;
};

#endif /* MC_COMPONENT_FILE_INPUT_H */
